﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataAccessLayer
{
    public partial class Guest
    {
        private bool isValid = true;
        private string errorMessage = string.Empty;

        public bool IsValid
        {
            get { return isValid; }
        }

        public string ErrorMessage
        {
            get { return errorMessage; }
        }

        partial void OnFirstNameChanging(string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                isValid = false;
                errorMessage += "Guest First Name cannot be blank" + Environment.NewLine;
            }
        }

        partial void OnLastNameChanging(string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                isValid = false;
                errorMessage += "Guest Last Name cannot be blank" + Environment.NewLine;
            }
        }

        public bool SetGuestInformation(Guest guest, bool includeCreditInfo)
        {
            if (string.IsNullOrEmpty(guest.FirstName))
            {
                SetError("Invalid first name.");
            }
            else
            {
                this.FirstName = guest.FirstName;
            }

            if (string.IsNullOrEmpty(guest.LastName))
            {
                SetError("Invalid last name.");
            }
            else
            {
                this.LastName = guest.LastName;
            }

            //if (includeCreditInfo)
            //{
            if (string.IsNullOrEmpty(guest.CreditNumber))
            {
                SetError("Invalid Credit Number.");
            }
            else
            {
                this.CreditNumber = guest.CreditNumber;
            }

            if (string.IsNullOrEmpty(guest.CVVNumber))
            {
                SetError("Invalid CVV number.");
            }
            else
            {
                this.CVVNumber = guest.CVVNumber;
            }

            if (guest.ExpirationDate != null && guest.ExpirationDate.Value.CompareTo(DateTime.Now) < 0)
            {
                SetError("Invalid expiry date.");
            }
            else
            {
                this.ExpirationDate = guest.ExpirationDate;
            }

            if (string.IsNullOrEmpty(guest.Country))
            {
                SetError("Invalid country.");
            }
            else
            {
                this.Country = guest.Country;
            }

            if (string.IsNullOrEmpty(guest.City))
            {
                SetError("Invalid city.");
            }
            else
            {
                this.City = guest.City;
            }

            if (string.IsNullOrEmpty(guest.Email) || !guest.Email.Contains("@"))
            {
                SetError("Invalid email address.");
            }
            else
            {
                this.Email = guest.Email;
            }
            //}

            return isValid;
        }
         
        private void SetError(string message)
        {
            //isValid = false;
            errorMessage += message + Environment.NewLine;
        }
    }
}
